package com.youngjun.user.service.impl;

import com.youngjun.common.exception.CommonExceptionType;
import com.youngjun.common.oss.expection.CommonException;
import com.youngjun.user.mapper.DepartmentMapper;
import com.youngjun.user.pojo.domain.Department;
import com.youngjun.user.pojo.dto.DepartmentDTO;
import com.youngjun.user.pojo.dto.query.DepartmentQueryDTO;
import com.youngjun.user.pojo.vo.DepartmentVO;
import com.youngjun.user.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    DepartmentMapper departmentMapper;

    @Override
    public DepartmentMapper getMapper() {
        return departmentMapper;
    }

    private List<DepartmentVO> recursionSuperList(List<DepartmentVO> list,Long id){
        if(id == null) {
            return list;
        }
        DepartmentVO vo = selectVO(new Department().setId(id), DepartmentVO.class);
        list.add(vo);
        return recursionSuperList(list,vo.getParentId());
    }

    private void recursionSubList(List<DepartmentVO> list,Long id){
        List<DepartmentVO> voList = selectAllVO(new Department().setParentId(id), DepartmentVO.class);
        for (DepartmentVO vo:voList) {
            list.add(vo);
            recursionSubList(list,vo.getId());
        }
    }

    @Override
    public List<DepartmentVO> querySuperListById(Long id) {
        DepartmentVO vo = selectVO(new Department().setId(id), DepartmentVO.class);
        List<DepartmentVO> list = new ArrayList<>();
        list.add(vo);
        return recursionSuperList(list,vo.getParentId());
    }

    @Override
    public List<DepartmentVO> querySubListById(Long id) {
        List<DepartmentVO> list = new ArrayList<>();
        recursionSubList(list,id);
        return list;
    }

    @Override
    public List<DepartmentVO> queryListByCurrentOrganization(DepartmentQueryDTO dto) {
        dto.setOrganizationId(1459153169673232384L);
        return queryList(dto);
    }

    @Override
    public List<DepartmentVO> queryList(DepartmentQueryDTO dto) {
        return selectAllVO(new Department().mapFrom(dto),DepartmentVO.class);
    }

    @Override
    public DepartmentVO createByCurrentOrganization(DepartmentDTO dto) {
        dto.setOrganizationId(1459153169673232384L);
        return create(dto);
    }

    @Override
    public DepartmentVO create(DepartmentDTO dto) {
        Department department =new Department().mapFrom(dto);
        insert(department);
        return new DepartmentVO().mapFrom(department);
    }

    @Override
    public DepartmentVO modifyByCurrentOrganization(DepartmentDTO dto) {
        dto.setOrganizationId(1459153169673232384L);
        return modify(dto);
    }

    @Override
    public DepartmentVO modify(DepartmentDTO dto) {
        Department department =new Department().mapFrom(dto);
        updateWithOptionalByPrimaryKey(department);
        return new DepartmentVO().mapFrom(department);
    }

    @Override
    public void deleteByCurrentOrganization(Long id) {
        Department department = new Department();
        department.setOrganizationId(1459153169673232384L);
        department.setId(id);
        if(select(department) == null){
            throw new CommonException(CommonExceptionType.DB_DELETE_FAILED);
        }
        delete(id);
    }

    @Override
    public void delete(Long id) {
        deleteWithLogic(id);
        //修改用户部门
    }

}